package com.xxxx.controller;

import com.xxxx.pojo.Admin;
import com.xxxx.pojo.AdminLoginParam;
import com.xxxx.pojo.RespBean;
import com.xxxx.service.IAdminRoleService;
import com.xxxx.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @program: yun2
 * @ClassName LoginController
 * @description:
 * @author: ZhouBin
 * @create: 2022-05-21 22:16
 * @Version 1.0
 **/
@Api(tags = "LoginController")
@RestController
public class LoginController {
    @Autowired
    private IAdminService adminService;
    @ApiOperation(value = "登入之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){
        return adminService.login(adminLoginParam.getUserName(),adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
    }
    @ApiOperation(value = "获取当前登入用户信息")
    @GetMapping(value = "/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (null==principal){
            return null;
        }
        String userName = principal.getName();
        Admin admin=adminService.getAdminByUserName(userName);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return admin;

    }

    @ApiOperation(value = "退出登入")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("退出成功");
    }

}
